What is snakeize?
The snakeize npm package is used to convert object keys to snake_case. This is particularly useful when working with APIs or databases that follow the snake_case naming convention.
What are snakeize's main functionalities?
Convert object keys to snake_case
This feature allows you to convert the keys of an object from camelCase or other formats to snake_case.
const snakeize = require('snakeize');
const input = { firstName: 'John', lastName: 'Doe' };
const output = snakeize(input);
console.log(output); // { first_name: 'John', last_name: 'Doe' }
Nested object key conversion
This feature allows you to convert the keys of nested objects to snake_case.
const snakeize = require('snakeize');
const input = { user: { firstName: 'John', lastName: 'Doe' }, age: 30 };
const output = snakeize(input);
console.log(output); // { user: { first_name: 'John', last_name: 'Doe' }, age: 30 }
Other packages similar to snakeize
camelcase-keys
The camelcase-keys package converts object keys to camelCase. It is similar to snakeize but focuses on converting keys to camelCase instead of snake_case.
decamelize-keys
The decamelize-keys package converts object keys from camelCase to another format, such as snake_case. It is similar to snakeize but offers more flexibility in the target format.
lodash
Lodash is a utility library that provides a wide range of functions for manipulating objects, arrays, and other data structures. It includes a method for converting object keys to snake_case, but it is a more general-purpose library compared to snakeize.
snakeize
recursively transform key strings from camel-case to underscore-style.
Derives directly from substack's camelize
example
var snakeize = require('snakeize');
var obj = {
feeFieFoe: 'fum',
beepBoop: [
{ 'abcXyz': 'mno' },
{ 'FooBar': 'baz' },
{ 'CheeseID': 'wensleydale' }
]
};
var res = snakeize(obj);
console.log(JSON.stringify(res, null, 2));
output:
{
"fee_fie_foe": "fum",
"beep_boop": [
{
"abc_xyz": "mno"
},
{
"foo_bar": "baz"
},
{
"cheese_id": "wensleydale"
}
]
}
methods
var snakeize = require('snakeize')
snakeize(obj)
Convert the key strings in obj
from camel-case to underscore-stlye recursively.
install
With npm do:
npm install snakeize
To use in the browser, use browserify or component:
component install nathan7/snakeize
license
MIT